from common_import import *


def detect_3sigma_outliers(data):
    # 获取列名
    column_names = data.dtype.names
    # total1, total2 = 0, 0
    for col in column_names[1:]:
        # 计算均值和标准差
        col_mean = np.mean(data[col])
        col_std = np.std(data[col])

        # 计算3-sigma区间
        lower_bound = col_mean - 3 * col_std
        upper_bound = col_mean + 3 * col_std

        # 查找超出3-sigma区间的值
        outliers = (data[col] < lower_bound) | (data[col] > upper_bound)
        # data[col][outliers] = col_mean

        # 输出异常值信息
        for row in data[outliers]:
            print(
                f"Row Name: {row[column_names[0]]}, Column: {col}, Outlier Value: {row[col]}"
            )
        # pass
        #     if len(outliers[outliers == True]) != 0:
        #         total1 += 1
        #         total2 += len(outliers[outliers == True])
        #         print(col, len(outliers[outliers == True]))
        # print(total1, total2)
    return data


def generate_NMN():
    data = tool.get_np("ADMET_training.csv")
    data["MN"] = 1 - data["MN"]
    data["hERG"] = 1 - data["hERG"]
    tool.get_csv(data, "ADMET_training.csv")


def max_string_length():
    max_length = 0
    string_array = tool.get_np("ADMET_test.csv")["SMILES"]
    for string in string_array:
        current_length = len(string)
        # print(current_length)
        if current_length > max_length:
            max_length = current_length
        if current_length == 300:
            print(string)
    print(max_length)
    return max_length


def count_variable_types():
    data = tool.get_np("Molecular_Descriptor_training.csv")
    float_count = 0
    int_count = 0
    binary_count = 0
    discrete_count = 0
    continuous_count = 0
    sparse_count = 0  # 90%为0的变量计数

    for dtype in data.dtype.descr:
        column_data = data[dtype[0]]
        zero_ratio = np.sum(column_data == 0) / len(column_data)  # 计算0值的比例

        if zero_ratio >= 0.9:  # 如果零值比例大于或等于90%
            sparse_count += 1

        if np.issubdtype(np.dtype(dtype[1]), np.floating):
            float_count += 1
            continuous_count += 1  # 假设所有浮点数都是连续的
        elif np.issubdtype(np.dtype(dtype[1]), np.integer):
            int_count += 1
            unique_values = np.unique(column_data)
            if set(unique_values).issubset({0, 1}):
                binary_count += 1
            elif len(unique_values) < 10:  # 假设10个及以下不同值的变量为离散变量
                discrete_count += 1
            else:
                continuous_count += 1

    print(f"浮点变量: {float_count}")
    print(f"整数变量: {int_count}")
    print(f"01变量: {binary_count}")
    print(f"离散变量(取值范围小于10种且不为01变量): {discrete_count}")
    print(f"连续变量: {continuous_count}")
    print(f"稀疏变量 (90%为0): {sparse_count}")


def check_pIC50(np_data):
    errors = []

    # 遍历数据中的每一行
    for row in np_data:
        smiles = row["SMILES"]
        ic50_nM = row["IC50_nM"]
        given_pIC50 = row["pIC50"]

        # 计算 pIC50
        ic50_M = ic50_nM * 1e-9  # 将 nM 转换为 M
        calculated_pIC50 = -np.log10(ic50_M)

        # 比较计算的 pIC50 和给定的 pIC50
        if not np.isclose(calculated_pIC50, given_pIC50, atol=1e-15):
            errors.append(smiles, ic50_M, given_pIC50, calculated_pIC50)
    print(errors)
    return errors


def juedge_equal():
    data1 = tool.get_np("ER_activity_training.csv")["SMILES"]
    print(len(data1))
    data2 = tool.get_np("ADMET_test.csv")["SMILES"]
    data3 = tool.get_np("Molecular_Descriptor_test.csv")["SMILES"]
    train1, val1 = train_test_split(data1, test_size=0.2, random_state=42)
    train2, val2 = train_test_split(data2, test_size=0.2, random_state=42)
    print(len(data1))
    print(len(train1), len(val1))


def check_missing_values():
    # 读取CSV文件
    data = pd.read_csv("data/Molecular_Descriptor_test.csv")

    # 检查是否有缺失值
    missing_values = data.isnull().sum()

    # 判断是否有缺失值
    if missing_values.any():
        print("The dataset contains missing values.")
        print("Missing values summary:")
        print(missing_values[missing_values > 0])  # 只显示有缺失值的列及其数量
        return missing_values[missing_values > 0]
    else:
        print("The dataset has no missing values.")
        return None


if __name__ == "__main__":
    pass
    check_missing_values()
